#!/usr/bin/env python3
import subprocess
from fire import Fire
from collections import defaultdict
from os.path import dirname
from datetime import date
import zd 
import sys

def extract(egin, end, html):
    if not html:
        return
    if egin is None:
        start = 0
    else:
        start = html.find(egin)
        if start >= 0:
            start += len(egin)+1
        else:
            return
    if end is not None:
        end = html.find(end, start)
    if end is None or end >= 0:
        return html[start:end].strip()

@Fire
def main(infile):

    with open("android.txt","rb") as android:
        exist = set(i.strip() for i in android)
    proc = subprocess.Popen(
        ['pigz',"-dc",infile],
        stdout=subprocess.PIPE,
        encoding=None
    )

    day_id = defaultdict(lambda:defaultdict(int)) 
    for pos, i in enumerate(proc.stdout):
        id = extract(b'AndroidId":',b'"',i)
        if id not in exist:
            continue
        day = int(extract(b'sessionTimestamp":',b'"',i) or 0)//86400000
        day_id[day][id]+=1
        #if pos > 1000000:
        #    break
    

    day_li = tuple(sorted(day_id.keys()))
    
    outfile = infile[:-3]+".csv.zst"
    utf8 = 'utf8'
    with zd.open(outfile,"w") as out:
        out.write(
            b"AndroidId"+b",".join(
                str(date.fromtimestamp(i)).encode(utf8) for i in day_li
            )
        )
        for id in exist:
            li = [day_id[day][id] for day in day_li]
            if any(li):
                out.write(id+b","+",".join(map(str,li)).encode(utf8)+b"\n")
